var controllers = angular.module('controllers',
                                 ['services', 'directives', 'ngDialog', 'gobals', 'ui.bootstrap']);

//主页
controllers.controller('IndexCtrl',
                       function ($scope, $rootScope, locationService, $location, gobalService,
                                 settingService) {

                           var nav = angular.element('nav');

                           if (nav) {
                               nav.hide();
                           }

                           //spinner6
                           var width = $('.fakeloader').width();
                           var height = $('.fakeloader').height();

                           jQuery(".spinner6").css({
                                                       'position': 'fixed',
                                                       'left': width / 2 - 35,
                                                       'top': height / 2 - 15
                                                   });

                           var cfg = gobalService.config();

                           if (isFromWx() && _obj.code && !$rootScope.openId) {
                               //换取access token 和 openId
                               settingService.authInfo(cfg.sysKey, cfg.appId, _obj.code)
                                   .then(function (authInfo) {

                                       if (authInfo.code == 0) {

                                           var openId = authInfo.data;

                                           $rootScope.openId = openId; //验证信息

                                           //跳转到业务界面
                                           navToBusiness();

                                       } else {
                                           $location.path('/index');//跳转到首页
                                       }
                                   }, function (error) {
                                       $location.path('/index');//跳转到首页
                                   });

                           } else {
                               //跳转到业务界面
                               navToBusiness();
                           }

                           function navToBusiness() {
                               //定位
                               locationService.locateMe($rootScope).then(function () {

                                   settingService.getServices(cfg.sysKey).then(function (result) {

                                       if (result.daijia) {
                                           $location.path('/daijia');
                                       }
                                       else if (result.zhuanche) {
                                           $location.path('/zhuanche');
                                       }
                                       else if (result.paotui) {
                                           $location.path('/delivery');
                                       }
                                       else if (result.xiche) {
                                           $location.path('/xiche');
                                       }

                                   });

                               }, function (error) {
                                   if (error.code == 1) {
                                       alert('没有权限使用地理定位API');
                                   } else if (error.code == 2) {
                                       alert('无法确定设备的位置');
                                   } else if (error.code == 3) {
                                       alert('定位超时');
                                   }
                               });
                           }

                       });

//代驾
controllers.controller('DaijiaCtrl',
                       function ($scope, $rootScope, $sce, $location, $uibModal, gobalService,
                                 locationService, daijiaService, userService, ngDialog) {

                           $scope.daijia = {};

                           var nav = angular.element('nav');

                           if (nav) {
                               nav.show();
                           }

                           angular.element('ul.nav_line > li > a').removeClass('nav_click');
                           angular.element('#daijia').addClass('nav_click');

                           var cfg = gobalService.config();

                           if (isFromWx()) {
                               if ($rootScope.openId) {
                                   userService.findWxBind(cfg.sysKey, $rootScope.openId)
                                       .then(function (result) {
                                           if (result.code == 0) {
                                               var bindData = result.data;
                                               $scope.daijia.phone = bindData.phone;
                                               $scope.daijia.passengerId = bindData.passengerId;

                                               $rootScope.passengerId = bindData.passengerId;
                                               $rootScope.passengerPhone = bindData.phone;
                                           }
                                       });
                               } else {
                                   $location.path('/index'); //跳转到首页
                               }

                           }

                           $scope.tip = $sce.trustAsHtml('<p>正在为您查询附近司机...</p>');
                           $scope.hideBeginTip = true;
                           $scope.hideEndTip = true;

                           $scope.hidePois = function () {

                               $scope.hideBeginTip = true;
                               $scope.hideEndTip = true;

                           }

                           /**
                            * 过滤不合法的poi
                            *
                            * @param results
                            * @returns {Array.<*>}
                            */
                           function filterPoi(results) {

                               var arr = [];
                               for (var i = 0; i < results.length; i++) {
                                   var poi = results[i];
                                   if (poi.location) {
                                       arr.push(poi);
                                   }
                               }

                               return arr.reverse();
                           }

                           /**
                            * 预算费用
                            *
                            * @param passengerId
                            * @param beginLat
                            * @param beginLng
                            * @param endLat
                            * @param endLng
                            */
                           function calculationFee(passengerId, beginLat, beginLng, endLat,
                                                   endLng) {

                               $scope.feemsg = $sce.trustAsHtml('正在为您预估费用...');

                               var promise = daijiaService.calculation(cfg.sysKey, cfg.ack,
                                                                       passengerId, beginLat,
                                                                       beginLng, endLat, endLng);
                               promise.then(function (result) {

                                   if (result.success) {

                                       $scope.feeDetail = result;

                                       $scope.feemsg =
                                           $sce.trustAsHtml(
                                               '约<b style="font-size: 34px;">' + result.money
                                               + '</b>元<img src="assets/v2/img/wenhao.png" width="24" height="24"/>');
                                   } else {

                                       $scope.feeDetail = null;
                                       $scope.feemsg = $sce.trustAsHtml('未能为您预估费用.');
                                   }

                               }, function (error) {
                                   $scope.feeDetail = null;
                                   $scope.feemsg = $sce.trustAsHtml('未能为您预估费用.');
                               });

                           }

                           $scope.showFeeDetail = function () {

                               if ($scope.feeDetail) {

                                   if ($scope.feeDetail.pay_detail) {
                                       $scope.feeDetail.detail =
                                           $sce.trustAsHtml(
                                               $scope.feeDetail.pay_detail.replace(/\n/g, "<br/>"));
                                   }
                                   //弹出支付提示
                                   ngDialog.open({
                                                     template: 'assets/v2/partials/feeInfo.html',
                                                     overlay: true,
                                                     showClose: true,
                                                     scope: $scope
                                                 });

                               }
                           }

                           /**
                            * 预约代驾订单
                            *
                            * @param beginAddress
                            * @param beginAddressDetail
                            * @param beginLat
                            * @param beginLng
                            * @param endAddress
                            * @param endAddressDetail
                            * @param endLat
                            * @param endLng
                            * @param phone
                            */
                           function makeAnAppointment(beginAddress, beginAddressDetail, beginLat,
                                                      beginLng, endAddress, endAddressDetail,
                                                      endLat, endLng, phone) {

                               var promise = daijiaService.makeAnAppointment(cfg.sysKey, cfg.ack,
                                                                             beginAddress,
                                                                             beginAddressDetail,
                                                                             beginLat, beginLng,
                                                                             endAddress,
                                                                             endAddressDetail,
                                                                             endLat, endLng, phone,
                                                                             $rootScope.openId);

                               promise.then(function (result) {

                                   if (result.success) {

                                       $location.path('/success').search({
                                                                             'orderId': result.orderId,
                                                                             'type': 'daijia'
                                                                         });

                                   } else {
                                       alert('下单失败');
                                   }

                               }, function (error) {

                                   alert('下单失败');

                               });
                           }

                           /**
                            * 初始化开始输入框
                            *
                            * @param locationInfo
                            */
                           function initBeginInput(locationInfo) {

                               $scope.daijia.beginAddress = locationInfo.name;
                               $scope.daijia.beginAddressDetail = locationInfo.address;
                               $scope.daijia.beginLat = locationInfo.point.lat;
                               $scope.daijia.beginLng = locationInfo.point.lng;

                               //开始地点
                               var stopBeginWatch = null;

                               $scope.beginSelect = function (poi) {

                                   $scope.daijia.beginAddress = poi.name;
                                   $scope.daijia.beginAddressDetail = poi.address;
                                   $scope.daijia.beginLat = poi.location.lat;
                                   $scope.daijia.beginLng = poi.location.lng;

                                   $scope.hideBeginTip = true;

                                   if ($scope.daijia.beginLat && $scope.daijia.beginLng
                                       && $scope.daijia.endLat && $scope.daijia.endLng) {
                                       calculationFee($scope.daijia.passengerId,
                                                      $scope.daijia.beginLat,
                                                      $scope.daijia.beginLng, $scope.daijia.endLat,
                                                      $scope.daijia.endLng);
                                   }

                               }

                               $scope.stopWatchBeginInput = function () {

                                   if (stopBeginWatch) {
                                       stopBeginWatch();
                                   }
                               }

                               $scope.watchBeginInput = function () {
                                   $scope.hideBeginTip = false;
                                   //开始地点
                                   stopBeginWatch =
                                       $scope.$watch('daijia.beginAddress', function (nv, ov) {
                                           if (nv != ov) {
                                               locationService.placeSearch(nv, locationInfo.city)
                                                   .then(function (resp) {
                                                       if (resp.data.status == 0) {
                                                           $scope.beginPois =
                                                               filterPoi(resp.data.results);
                                                       }
                                                   });
                                           }
                                       });
                               }

                           }

                           /**
                            * 初始化结束输入框
                            *
                            * @param locationInfo
                            */
                           function initEndInput(locationInfo) {

                               //结束地点
                               var stopEndWatch = null;

                               $scope.endSelect = function (poi) {

                                   $scope.daijia.endAddress = poi.name;
                                   $scope.daijia.endAddressDetail = poi.address;
                                   $scope.daijia.endLat = poi.location.lat;
                                   $scope.daijia.endLng = poi.location.lng;

                                   $scope.hideEndTip = true;

                                   if ($scope.daijia.beginLat && $scope.daijia.beginLng
                                       && $scope.daijia.endLat && $scope.daijia.endLng) {
                                       calculationFee($scope.daijia.passengerId,
                                                      $scope.daijia.beginLat,
                                                      $scope.daijia.beginLng, $scope.daijia.endLat,
                                                      $scope.daijia.endLng);
                                   }
                               }

                               $scope.stopWatchEndInput = function () {

                                   if (stopEndWatch) {
                                       stopEndWatch();
                                   }
                               }
                               $scope.watchEndInput = function () {
                                   $scope.hideEndTip = false;
                                   //开始地点
                                   stopEndWatch =
                                       $scope.$watch('daijia.endAddress', function (nv, ov) {
                                           if (nv != ov) {
                                               locationService.placeSearch(nv, locationInfo.city)
                                                   .then(function (resp) {
                                                       if (resp.data.status == 0) {
                                                           $scope.endPois =
                                                               filterPoi(resp.data.results);
                                                       }
                                                   });
                                           }
                                       });
                               }

                           }

                           /**
                            * 查询附近司机
                            *
                            * @param locationInfo
                            */
                           function coutNearDrivers(locationInfo) {

                               //查询附近司机人数
                               var promise = daijiaService.coutNearDrivers(cfg.sysKey,
                                                                           locationInfo.point.lat,
                                                                           locationInfo.point.lng);

                               promise.then(function (result) {

                                   if (result.count != 0) {
                                       $scope.tip =
                                           $sce.trustAsHtml('<h1>您好，您的附近有<b>' + result.count
                                                            + '</b>名司机</h1><p>最近司机距离您<b>'
                                                            + result.distance + '</b></p>');
                                   } else {
                                       $scope.tip = $sce.trustAsHtml('您的附近暂时没有司机');
                                   }

                               });

                           }

                           var promise = locationService.locateMe($rootScope);

                           promise.then(function (locationInfo) {

                               coutNearDrivers(locationInfo);

                               initBeginInput(locationInfo);

                               initEndInput(locationInfo);

                               //立即预约
                               $scope.submit = function () {

                                   //验证表单
                                   var params = $scope.daijia;

                                   if (!params.beginAddress || !params.beginLat
                                       || !params.beginLng) {
                                       alert('请先输入并选择预约地址');
                                       return;
                                   }
                                   if (!params.endAddress || !params.endLat || !params.endLng) {
                                       alert('请先输入并选择目的地');
                                       return;
                                   }

                                   if (!params.phone) {
                                       alert('请先输入手机号码');
                                       return;
                                   }
                                   if (!(/^0?1\d{10}$/.test(params.phone))) {
                                       alert('请先输入正确的手机号码');
                                       return;
                                   }

                                   //验证手机号是否已经绑定,未绑定发送验证码
                                   var promise = userService.checkBind(cfg.appId, cfg.sysKey,
                                                                       params.phone,
                                                                       $rootScope.openId);

                                   promise.then(function (result) {

                                       if (result.isBind) {
                                           //执行下单逻辑
                                           makeAnAppointment(params.beginAddress,
                                                             params.beginAddressDetail,
                                                             params.beginLat, params.beginLng,
                                                             params.endAddress,
                                                             params.endAddressDetail, params.endLat,
                                                             params.endLng, params.phone);

                                       }
                                       else if (result.isSend) {
                                           //发送验证码,验证验证码
                                           //https://github.com/likeastore/ngDialog
                                           //跳转登录界面
                                           var dialog = ngDialog.open({
                                                                          template: 'assets/v2/partials/validatecode.html',
                                                                          showClose: false,
                                                                          overlay: true,
                                                                          controller: function ($scope,
                                                                                                params) {

                                                                              $scope.phone =
                                                                                  params.phone;

                                                                              $scope.validate =
                                                                                  function () {
                                                                                      //验证信息
                                                                                      if (!$scope.vcode) {
                                                                                          alert(
                                                                                              '请先输入验证码信息');
                                                                                          return;
                                                                                      }

                                                                                      userService.checkCode(
                                                                                          cfg.appId,
                                                                                          cfg.sysKey,
                                                                                          $scope.phone,
                                                                                          $scope.vcode,
                                                                                          $rootScope.openId,
                                                                                          $rootScope.currentLocation.city)
                                                                                          .then(
                                                                                              function (resp) {
                                                                                                  if (resp.success) {
                                                                                                      //验证成功
                                                                                                      dialog.close(
                                                                                                          true);
                                                                                                  } else {
                                                                                                      alert(
                                                                                                          resp.message);
                                                                                                  }

                                                                                              },
                                                                                              function (error) {
                                                                                                  alert(
                                                                                                      '验证失败,请稍后再试');
                                                                                              });
                                                                                  }

                                                                          },
                                                                          resolve: {
                                                                              params: function () {
                                                                                  return $scope.daijia;
                                                                              }
                                                                          }
                                                                      });

                                           dialog.closePromise.then(function (data) {
                                               //执行下单逻辑
                                               if (data.value == true) {
                                                   makeAnAppointment(params.beginAddress,
                                                                     params.beginAddressDetail,
                                                                     params.beginLat,
                                                                     params.beginLng,
                                                                     params.endAddress,
                                                                     params.endAddressDetail,
                                                                     params.endLat, params.endLng,
                                                                     params.phone);
                                               }
                                           });

                                       } else {
                                           alert('验证码发送失败,请稍后再试');
                                       }
                                   });
                               }

                           }, function (error) {
                               if (error.code == 1) {
                                   alert('没有权限使用地理定位API');
                               } else if (error.code == 2) {
                                   alert('无法确定设备的位置');
                               } else if (error.code == 3) {
                                   alert('定位超时');
                               }
                           });

                       });

//专车
controllers.controller('ZhuanCheCtrl',
                       function ($scope, $rootScope, $sce, $location, gobalService, locationService,
                                 zhuancheService, userService, ngDialog) {

                           var cfg = gobalService.config();

                           var nav = angular.element('nav');

                           if (nav) {
                               nav.show();
                           }
                           angular.element('ul.nav_line > li > a').removeClass('nav_click');
                           angular.element('#zhuanche').addClass('nav_click');

                           $scope.zhuanche = {};

                           if (isFromWx()) {
                               if ($rootScope.openId) {
                                   userService.findWxBind(cfg.sysKey, $rootScope.openId)
                                       .then(function (result) {
                                           if (result.code == 0) {
                                               var bindData = result.data;
                                               $scope.zhuanche.phone = bindData.phone;
                                               $scope.zhuanche.passengerId = bindData.passengerId;

                                               $rootScope.passengerId = bindData.passengerId;
                                               $rootScope.passengerPhone = bindData.phone;
                                           }
                                       });
                               } else {
                                   $location.path('/index'); //跳转到首页
                               }

                           }

                           $scope.tip = $sce.trustAsHtml('<p>正在为您查询附近专车...</p>');
                           $scope.hideBeginTip = true;
                           $scope.hideEndTip = true;

                           $scope.hidePois = function () {

                               $scope.hideBeginTip = true;
                               $scope.hideEndTip = true;

                           }

                           /**
                            * 过滤不合法的poi
                            *
                            * @param results
                            * @returns {Array.<*>}
                            */
                           function filterPoi(results) {

                               var arr = [];
                               for (var i = 0; i < results.length; i++) {
                                   var poi = results[i];
                                   if (poi.location) {
                                       arr.push(poi);
                                   }
                               }

                               return arr.reverse();
                           }

                           /**
                            * 预算费用
                            *
                            * @param passengerId
                            * @param beginLat
                            * @param beginLng
                            * @param endLat
                            * @param endLng
                            */
                           function calculationFee(carTypeId, beginLat, beginLng, endLat, endLng) {

                               $scope.feemsg = $sce.trustAsHtml('正在为您预估费用...');

                               var promise = zhuancheService.calculation(cfg.sysKey, carTypeId,
                                                                         beginLat, beginLng, endLat,
                                                                         endLng);

                               promise.then(function (result) {

                                   if (result.success) {
                                       $scope.feeDetail = result;
                                       $scope.feemsg =
                                           $sce.trustAsHtml(
                                               '约<b style="font-size: 34px;">' + result.money
                                               + '</b>元<img src="assets/v2/img/wenhao.png" width="24" height="24"/>');
                                   } else {
                                       $scope.feeDetail = null;
                                       $scope.feemsg = $sce.trustAsHtml('未能为您预估费用.');
                                   }

                               }, function (error) {
                                   $scope.feeDetail = null;
                                   $scope.feemsg = $sce.trustAsHtml('未能为您预估费用.');
                               });

                           }

                           $scope.showFeeDetail = function () {

                               if ($scope.feeDetail) {

                                   if ($scope.feeDetail.pay_detail) {
                                       $scope.feeDetail.detail =
                                           $sce.trustAsHtml(
                                               $scope.feeDetail.pay_detail.replace(/\n/g, "<br/>"));
                                   }
                                   //弹出支付提示
                                   ngDialog.open({
                                                     template: 'assets/v2/partials/feeInfo.html',
                                                     overlay: true,
                                                     showClose: true,
                                                     scope: $scope
                                                 });

                               }
                           }

                           /**
                            * 预约专车订单
                            *
                            * @param beginAddress
                            * @param beginAddressDetail
                            * @param beginLat
                            * @param beginLng
                            * @param endAddress
                            * @param endAddressDetail
                            * @param endLat
                            * @param endLng
                            * @param phone
                            * @param carTypeId
                            */
                           function makeAnAppointment(beginAddress, beginAddressDetail, beginLat,
                                                      beginLng, endAddress, endAddressDetail,
                                                      endLat, endLng, phone, carTypeId) {

                               var promise = zhuancheService.makeAnAppointment(cfg.sysKey, cfg.ack,
                                                                               beginAddress,
                                                                               beginAddressDetail,
                                                                               beginLat, beginLng,
                                                                               endAddress,
                                                                               endAddressDetail,
                                                                               endLat, endLng,
                                                                               phone,
                                                                               $rootScope.openId,
                                                                               carTypeId);

                               promise.then(function (result) {

                                   if (result.success) {

                                       $location.path('/success').search({
                                                                             'orderId': result.orderId,
                                                                             'type': 'zhuanche'
                                                                         });

                                   } else {
                                       alert('下单失败');
                                   }

                               }, function (error) {

                                   alert('下单失败');

                               });
                           }

                           //选择列表开关
                           $scope.hideCarTypeList = true;

                           $scope.showSelect = function () {
                               $scope.hideCarTypeList = false;
                           }

                           /**
                            * 选择专车
                            *
                            * @param carType
                            */
                           $scope.selectCarType = function (carType) {
                               $scope.zhuanche.carType = carType;
                               $scope.hideCarTypeList = true;

                               //查询附近司机
                               countNearCars($rootScope.currentLocation.point.lat,
                                             $rootScope.currentLocation.point.lng,
                                             $scope.zhuanche.carType.carTypeId);
                               //重新预算价格
                               if ($scope.zhuanche.beginLat && $scope.zhuanche.beginLng
                                   && $scope.zhuanche.endLat && $scope.zhuanche.endLng
                                   && $scope.zhuanche.carType) {
                                   calculationFee($scope.zhuanche.carType.carTypeId,
                                                  $scope.zhuanche.beginLat,
                                                  $scope.zhuanche.beginLng, $scope.zhuanche.endLat,
                                                  $scope.zhuanche.endLng);
                               }
                           }

                           /**
                            * 根据车辆类型与位置查询附近车辆人数
                            *
                            * @param lat
                            * @param lng
                            * @param carTypeId
                            */
                           function countNearCars(lat, lng, carTypeId) {

                               //查询附近司机人数
                               var promise = zhuancheService.coutNearDrivers(cfg.sysKey, lat, lng,
                                                                             carTypeId);

                               promise.then(function (result) {

                                   if (result.count != 0) {
                                       $scope.tip =
                                           $sce.trustAsHtml('<h1>您好，您的附近有<b>' + result.count
                                                            + '</b>辆专车</h1><p>最近专车距离您<b>'
                                                            + result.distance + '</b></p>');
                                   } else {
                                       $scope.tip = $sce.trustAsHtml('您的附近暂时没有专车');
                                   }

                               });

                           }

                           /**
                            * 初始化开始地址选择框
                            *
                            * @param locationInfo
                            */
                           function initBeginInput(locationInfo) {

                               $scope.zhuanche.beginAddress = locationInfo.name;
                               $scope.zhuanche.beginAddressDetail = locationInfo.address;
                               $scope.zhuanche.beginLat = locationInfo.point.lat;
                               $scope.zhuanche.beginLng = locationInfo.point.lng;

                               //开始地点
                               var stopBeginWatch = null;

                               $scope.beginSelect = function (poi) {

                                   $scope.zhuanche.beginAddress = poi.name;
                                   $scope.zhuanche.beginAddressDetail = poi.address;
                                   $scope.zhuanche.beginLat = poi.location.lat;
                                   $scope.zhuanche.beginLng = poi.location.lng;

                                   $scope.hideBeginTip = true;

                                   if ($scope.zhuanche.beginLat && $scope.zhuanche.beginLng
                                       && $scope.zhuanche.endLat && $scope.zhuanche.endLng
                                       && $scope.zhuanche.carType) {
                                       calculationFee($scope.zhuanche.carType.carTypeId,
                                                      $scope.zhuanche.beginLat,
                                                      $scope.zhuanche.beginLng,
                                                      $scope.zhuanche.endLat,
                                                      $scope.zhuanche.endLng);
                                   }
                               }

                               $scope.stopWatchBeginInput = function () {

                                   if (stopBeginWatch) {
                                       stopBeginWatch();
                                   }
                               }

                               $scope.watchBeginInput = function () {
                                   $scope.hideBeginTip = false;
                                   //开始地点
                                   stopBeginWatch =
                                       $scope.$watch('zhuanche.beginAddress', function (nv, ov) {
                                           if (nv != ov) {
                                               locationService.placeSearch(nv, locationInfo.city)
                                                   .then(function (resp) {
                                                       if (resp.data.status == 0) {
                                                           $scope.beginPois =
                                                               filterPoi(resp.data.results);
                                                       }
                                                   });
                                           }
                                       });
                               }
                           }

                           function initEndInput(locationInfo) {

                               //结束地点
                               var stopEndWatch = null;

                               $scope.endSelect = function (poi) {

                                   $scope.zhuanche.endAddress = poi.name;
                                   $scope.zhuanche.endAddressDetail = poi.address;
                                   $scope.zhuanche.endLat = poi.location.lat;
                                   $scope.zhuanche.endLng = poi.location.lng;

                                   $scope.hideEndTip = true;

                                   if ($scope.zhuanche.beginLat && $scope.zhuanche.beginLng
                                       && $scope.zhuanche.endLat && $scope.zhuanche.endLng
                                       && $scope.zhuanche.carType) {
                                       calculationFee($scope.zhuanche.carType.carTypeId,
                                                      $scope.zhuanche.beginLat,
                                                      $scope.zhuanche.beginLng,
                                                      $scope.zhuanche.endLat,
                                                      $scope.zhuanche.endLng);
                                   }
                               }

                               $scope.stopWatchEndInput = function () {

                                   if (stopEndWatch) {
                                       stopEndWatch();
                                   }
                               }
                               $scope.watchEndInput = function () {
                                   $scope.hideEndTip = false;
                                   //开始地点
                                   stopEndWatch =
                                       $scope.$watch('zhuanche.endAddress', function (nv, ov) {
                                           if (nv != ov) {
                                               locationService.placeSearch(nv, locationInfo.city)
                                                   .then(function (resp) {
                                                       if (resp.data.status == 0) {
                                                           $scope.endPois =
                                                               filterPoi(resp.data.results);
                                                       }
                                                   });
                                           }
                                       });
                               }
                           }

                           var promise = locationService.locateMe($rootScope);

                           promise.then(function (locationInfo) {

                               //查询车辆类型
                               var promise = zhuancheService.getCarTypes(cfg.sysKey, cfg.ack,
                                                                         $scope.zhuanche.passengerId,
                                                                         locationInfo.point.lat,
                                                                         locationInfo.point.lng);

                               promise.then(function (carTypes) {

                                   $scope.carTypes = carTypes;

                                   if (carTypes.length > 0) {
                                       $scope.zhuanche.carType = carTypes[0];
                                   }

                                   //查询附近司机
                                   countNearCars(locationInfo.point.lat, locationInfo.point.lng,
                                                 $scope.zhuanche.carType.carTypeId);

                                   //初始化化开始选择框
                                   initBeginInput(locationInfo);

                                   //初始化结束选择框
                                   initEndInput(locationInfo);
                               });

                               //立即预约
                               $scope.submit = function () {

                                   //验证表单
                                   var params = $scope.zhuanche;

                                   if (!params.beginAddress || !params.beginLat
                                       || !params.beginLng) {
                                       alert('请先输入并选择预约地址');
                                       return;
                                   }
                                   if (!params.endAddress || !params.endLat || !params.endLng) {
                                       alert('请先输入并选择目的地');
                                       return;
                                   }

                                   if (!params.phone) {
                                       alert('请先输入手机号码');
                                       return;
                                   }

                                   if (!(/^0?1\d{10}$/.test(params.phone))) {
                                       alert('请先输入正确的手机号码');
                                       return;
                                   }

                                   if (!params.carType) {
                                       alert('请选择预约车型');
                                       return;
                                   }

                                   //验证手机号是否已经绑定,未绑定发送验证码
                                   var promise = userService.checkBind(cfg.appId, cfg.sysKey,
                                                                       params.phone,
                                                                       $rootScope.openId);

                                   promise.then(function (result) {
                                       if (result.isBind) {
                                           //执行下单逻辑
                                           makeAnAppointment(params.beginAddress,
                                                             params.beginAddressDetail,
                                                             params.beginLat, params.beginLng,
                                                             params.endAddress,
                                                             params.endAddressDetail, params.endLat,
                                                             params.endLng, params.phone,
                                                             params.carType.carTypeId);

                                       }
                                       else if (result.isSend) {
                                           //发送验证码,验证验证码
                                           //跳转登录界面
                                           var dialog = ngDialog.open({
                                                                          template: 'assets/v2/partials/validatecode.html',
                                                                          showClose: false,
                                                                          overlay: true,
                                                                          controller: function ($scope,
                                                                                                params) {

                                                                              $scope.phone =
                                                                                  params.phone;

                                                                              $scope.validate =
                                                                                  function () {
                                                                                      //验证信息
                                                                                      if (!$scope.vcode) {
                                                                                          alert(
                                                                                              '请先输入验证码信息');
                                                                                          return;
                                                                                      }

                                                                                      userService.checkCode(
                                                                                          cfg.appId,
                                                                                          cfg.sysKey,
                                                                                          $scope.phone,
                                                                                          $scope.vcode,
                                                                                          $rootScope.openId,
                                                                                          $rootScope.currentLocation.city)
                                                                                          .then(
                                                                                              function (resp) {
                                                                                                  if (resp.success) {
                                                                                                      //验证成功
                                                                                                      dialog.close(
                                                                                                          true);
                                                                                                  } else {
                                                                                                      alert(
                                                                                                          resp.message);
                                                                                                  }

                                                                                              },
                                                                                              function (error) {
                                                                                                  alert(
                                                                                                      '验证失败,请稍后再试');
                                                                                              });
                                                                                  }

                                                                          },
                                                                          resolve: {
                                                                              params: function () {
                                                                                  return $scope.zhuanche;
                                                                              }
                                                                          }
                                                                      });

                                           dialog.closePromise.then(function (data) {
                                               //执行下单逻辑
                                               if (data.value == true) {
                                                   makeAnAppointment(params.beginAddress,
                                                                     params.beginAddressDetail,
                                                                     params.beginLat,
                                                                     params.beginLng,
                                                                     params.endAddress,
                                                                     params.endAddressDetail,
                                                                     params.endLat, params.endLng,
                                                                     params.phone,
                                                                     params.carType.carTypeId);
                                               }
                                           });

                                       } else {
                                           alert('验证码发送失败,请稍后再试');
                                       }
                                   });
                               }

                           }, function (error) {
                               if (error.code == 1) {
                                   alert('没有权限使用地理定位API');
                               } else if (error.code == 2) {
                                   alert('无法确定设备的位置');
                               } else if (error.code == 3) {
                                   alert('定位超时');
                               }
                           });

                       });

//跑腿取送控制器
controllers.controller('DeliveryCtrl',
                       function ($scope, $rootScope, $sce, $uibModal, $filter, $location,
                                 gobalService, paotuiService, locationService, userService) {

                           $scope.task = {};

                           var nav = angular.element('nav');

                           if (nav) {
                               nav.show();
                           }
                           angular.element('ul.nav_line > li > a').removeClass('nav_click');
                           angular.element('#delivery').addClass('nav_click');

                           var cfg = gobalService.config();

                           if (isFromWx()) {
                               if ($rootScope.openId) {
                                   userService.findWxBind(cfg.sysKey, $rootScope.openId)
                                       .then(function (result) {
                                           if (result.code == 0) {
                                               var bindData = result.data;
                                               $rootScope.passengerId = bindData.passengerId;
                                               $rootScope.passengerPhone = bindData.phone;

                                               init();

                                           } else {
                                               //跳转登录界面
                                               $location.path('/login').search({
                                                                                   'refer': 'delivery'
                                                                               });
                                           }
                                       });
                               } else {
                                   $location.path('/index'); //跳转到首页
                               }

                           } else if (!$rootScope.passengerId) {
                               //跳转登录页面
                               $location.path('/login').search({
                                                                   'refer': 'delivery'
                                                               });

                           } else {
                               init();
                           }

                           //预算价格
                           function budget(locationInfo) {

                               if ($scope.task.startAddress && $scope.task.endAddress) {

                                   $scope.feemsg = "正在为您预估价格,请稍等...";
                                   //两个值都有的时候
                                   var promise = paotuiService.budget(cfg.sysKey, cfg.ack,
                                                                      locationInfo.city,
                                                                      $rootScope.passengerId,
                                                                      $scope.task.startLat,
                                                                      $scope.task.startLng,
                                                                      $scope.task.endLat,
                                                                      $scope.task.endLng);

                                   promise.then(function (result) {

                                       $scope.budget = result;

                                       if (result.success) {
                                           $scope.feemsg =
                                               $sce.trustAsHtml('<span>约<b>' + result.money
                                                                + '</b>元<img src="assets/v2/img/d-wh.png"/></span>');
                                       } else {
                                           $scope.feemsg = $sce.trustAsHtml('未能为您预估费用.');
                                       }

                                   }, function (error) {

                                       $scope.feemsg = $sce.trustAsHtml('未能为您预估费用.');

                                   });
                               }
                           }

                           //初始化功能
                           function init() {

                               $scope.task.startPhone = $rootScope.passengerPhone;
                               $scope.task.passengerId = $rootScope.passengerId;

                               var promise = locationService.locateMe($rootScope);

                               promise.then(function (locationInfo) {

                                   //初始化数据
                                   $scope.task.startDate = new Date();
                                   $scope.task.startTime =
                                       $filter('date')($scope.task.startDate, 'yyyy-MM-dd HH:mm');
                                   $scope.task.startAddressDetail = locationInfo.address;
                                   $scope.task.startAddress = locationInfo.name;
                                   $scope.task.startLat = locationInfo.point.lat;
                                   $scope.task.startLng = locationInfo.point.lng;

                                   //选择时间
                                   $scope.showTimePicker = function () {

                                       var modalInstance = $uibModal.open({
                                                                              templateUrl: 'assets/v2/partials/datetimepicker.html',
                                                                              controller: 'DateTimePickerCtrl',
                                                                          });

                                       modalInstance.result.then(function (result) {
                                           $scope.task.startDate = result;//日期格式
                                           $scope.task.startTime =
                                               $filter('date')(result, 'yyyy-MM-dd HH:mm')
                                       });
                                   }

                                   //选择地址
                                   $scope.showAddressPicker = function () {

                                       var modalInstance = $uibModal.open({
                                                                              templateUrl: 'assets/v2/partials/addresspicker.html',
                                                                              controller: 'AddressPickerCtrl',
                                                                          });

                                       modalInstance.result.then(function (result) {
                                           $scope.task.startAddress = result.name;
                                           $scope.task.startLat = result.location.lat;
                                           $scope.task.startLng = result.location.lng;
                                           $scope.task.startAddressDetail = result.address;//具体地址名

                                           budget(locationInfo);//预算价格
                                       });
                                   }

                                   //选择交件时间
                                   $scope.showTimePicker2 = function () {

                                       var modalInstance = $uibModal.open({
                                                                              templateUrl: 'assets/v2/partials/datetimepicker.html',
                                                                              controller: 'DateTimePickerCtrl',
                                                                          });

                                       modalInstance.result.then(function (result) {
                                           $scope.task.endDate = result;//日期格式
                                           $scope.task.endTime =
                                               $filter('date')(result, 'yyyy-MM-dd HH:mm')
                                       });
                                   }

                                   //选择交件地址
                                   $scope.showAddressPicker2 = function () {

                                       var modalInstance = $uibModal.open({
                                                                              templateUrl: 'assets/v2/partials/addresspicker.html',
                                                                              controller: 'AddressPickerCtrl',
                                                                          });

                                       modalInstance.result.then(function (result) {

                                           $scope.task.endLat = result.location.lat;
                                           $scope.task.endLng = result.location.lng;
                                           $scope.task.endAddressDetail = result.address;//具体地址名
                                           $scope.task.endAddress = result.name;

                                           budget(locationInfo);//预算价格
                                       });
                                   }

                                   //显示估价界面
                                   $scope.showPriceDetail = function () {

                                       var modalInstance = $uibModal.open({
                                                                              templateUrl: 'assets/v2/partials/info.html',
                                                                              controller: 'InfoCtrl',
                                                                              resolve: {
                                                                                  content: function () {
                                                                                      return 'hello world';
                                                                                  }
                                                                              }
                                                                          });
                                   }

                                   $scope.submit = function () {

                                       var msg = null;

                                       var params = $scope.task;

                                       if (!params.title || params.title.length == 0) {
                                           msg = '请先输入任务标题';
                                       } else if (!params.content || params.content.length == 0) {
                                           msg = '请先输入任务详情';
                                       } else if (!params.startTime || params.startTime.length
                                                                       == 0) {
                                           msg = '请先输入取件时间';
                                       } else if (!params.startPhone || params.startPhone.length
                                                                        == 0) {
                                           msg = '请先输入取件手机号码';
                                       } else if (!params.startAddress || params.startAddress.length
                                                                          == 0) {
                                           msg = '请先输入取件地址';
                                       } else if (!params.endTime || params.endTime.length == 0) {
                                           msg = '请先输入交件时间';
                                       } else if (!params.endPhone || params.endPhone.length == 0) {
                                           msg = '请先输入交件电话';
                                       } else if (!params.endAddress || params.endAddress.length
                                                                        == 0) {
                                           msg = '请先输入交件地址';
                                       } else if (params.startDate >= params.endDate) {
                                           msg = '交件时间必须大于取件时间';
                                       }

                                       if (msg) {
                                           alert(msg);
                                           return;
                                       }

                                       var deliverAddress = params.endAddress;
                                       if (params.endAddressDetail) {
                                           deliverAddress =
                                               deliverAddress + "," + params.endAddressDetail;
                                       }

                                       var startAddress = params.startAddress;
                                       if (params.startAddressDetail) {
                                           startAddress =
                                               startAddress + "," + params.startAddressDetail;
                                       }

                                       var pro = paotuiService.makeAnAppointment(params.title,
                                                                                 params.content,
                                                                                 params.endTime,
                                                                                 deliverAddress,
                                                                                 params.endLat,
                                                                                 params.endLng,
                                                                                 $rootScope.passengerPhone,
                                                                                 $rootScope.openId,
                                                                                 $rootScope.passengerId,
                                                                                 $scope.budget.money,
                                                                                 params.startTime,
                                                                                 params.startPhone,
                                                                                 params.endPhone,
                                                                                 startAddress,
                                                                                 params.startLat,
                                                                                 params.startLng,
                                                                                 'qusong',
                                                                                 $scope.budget.travelTime,
                                                                                 $scope.budget.mileage,
                                                                                 $scope.budget.startPrice,
                                                                                 $scope.budget.mileagePrice,
                                                                                 $scope.budget.travelTimePrice,
                                                                                 cfg.sysKey);

                                       pro.then(function (result) {
                                           if (result.code == 0) {
                                               alert('下单成功');
                                           } else {
                                               alert('下单失败');
                                           }

                                       }, function (error) {
                                           alert('下单失败,请稍候再试');
                                       });

                                       //if(msg){
                                       //    var modalInstance = $uibModal.open({
                                       //        templateUrl: 'assets/v2/partials/info.html',
                                       //        controller: 'InfoCtrl',
                                       //        resolve: {
                                       //            content : function(){
                                       //                return msg;
                                       //            }
                                       //        }
                                       //    });
                                       //    return;
                                       //}
                                   }

                               }, function (error) {
                                   if (error.code == 1) {
                                       alert('没有权限使用地理定位API');
                                   } else if (error.code == 2) {
                                       alert('无法确定设备的位置');
                                   } else if (error.code == 3) {
                                       alert('定位超时');
                                   }
                               });
                           }

                       });

//下单成功
controllers.controller('SuccessCtrl',
                       function ($scope, $rootScope, $routeParams, $location, $filter,
                                 daijiaService, zhuancheService, gobalService) {

                           var cfg = gobalService.config();

                           var nav = angular.element('nav');

                           if (nav) {
                               nav.hide();
                           }

                           /**
                            * 查询代驾订单
                            */
                           function findDJOrder() {

                               var promise = daijiaService.findOrder(cfg.sysKey,
                                                                     $routeParams.orderId);

                               promise.then(function (result) {
                                   if (result.success) {
                                       var orderInfo = result.data;

                                       $scope.bookTime = orderInfo.serverTime;
                                       $scope.beginAddress = orderInfo.fromPlace;
                                       $scope.endAddress = orderInfo.toPlace;
                                       $scope.phone = orderInfo.clientPhone;

                                       if (orderInfo.status == 5) { //已经销单
                                           //跳转到主页
                                           $location.path('/index');
                                           $location.replace();
                                       }
                                       else if (orderInfo.status == 0 || orderInfo.status == 1) {

                                           setTimeout(findDJOrder, 1000);//请求成功间隔一秒再次请求

                                       }
                                       else if (orderInfo.status == 2) { //司机已接单

                                           if (orderInfo.treatment) { //司机已经确认支付信息
                                               //跳转到支付界面
                                               $location.path('/payment').search({
                                                                                     'orderId': $routeParams.orderId,
                                                                                     'type': $routeParams.type
                                                                                 });
                                               return;
                                           }

                                           //跳转接单界面
                                           $location.path('/execute', {
                                               'orderId': $routeParams.orderId,
                                               'type': $routeParams.type
                                           });

                                       }
                                       else if (orderInfo.status == 3 || orderInfo.status == 6) { //已完成
                                           //跳转完成界面
                                           alert('订单已完成');
                                           $location.path("/index");
                                       }

                                   }

                               }, function (error) {

                                   setTimeout(findDJOrder, 1000);//请求成功间隔一秒再次请求

                               });

                           }

                           /**
                            * 查询专车订单
                            */
                           function findZCOrder() {

                               var promise = zhuancheService.findOrder(cfg.sysKey,
                                                                       $routeParams.orderId);

                               promise.then(function (result) {

                                   if (result.success) {

                                       var orderInfo = result.data;

                                       $scope.bookTime = orderInfo.serverTime;
                                       $scope.beginAddress = orderInfo.startAddress;
                                       $scope.endAddress = orderInfo.endAddress;
                                       $scope.phone = orderInfo.passengerPhone;

                                       if (orderInfo.status == 6) { //已经销单
                                           //跳转到主页
                                           $location.path('/index');
                                           $location.replace();
                                       }
                                       else if (orderInfo.status == 0 || orderInfo.status == 1) {

                                           setTimeout(findZCOrder, 1000);//请求成功间隔一秒再次请求

                                       }
                                       else if (orderInfo.status == 2 || orderInfo.status == 3
                                                || orderInfo.status == 4) { //司机已接单

                                           if (orderInfo.status == 4 && orderInfo.treatment) { //司机已经确认支付信息
                                               //跳转到支付界面
                                               $location.path('/payment').search({
                                                                                     'orderId': $routeParams.orderId,
                                                                                     'type': $routeParams.type
                                                                                 });
                                               return;
                                           }

                                           //跳转接单界面
                                           $location.path('/execute', {
                                               'orderId': $routeParams.orderId,
                                               'type': $routeParams.type
                                           });

                                       }
                                       else if (orderInfo.status == 5) { //已完成
                                           //跳转完成界面
                                           alert('订单已完成');
                                           $location.path("/index");
                                       }

                                   }

                               }, function (error) {
                                   setTimeout(findZCOrder, 1000);//请求成功间隔一秒再次请求
                               });

                           }

                           if ($routeParams.type == 'daijia') {

                               findDJOrder();

                           } else if ($routeParams.type == 'zhuanche') {

                               findZCOrder();

                           }

                           $scope.cancel = function () {

                               if ($routeParams.type == 'daijia') {
                                   //取消代驾
                                   $location.path('/cancel').search({
                                                                        'orderId': $routeParams.orderId,
                                                                        'type': $routeParams.type
                                                                    });
                               }
                               else if ($routeParams.type == 'zhuanche') {
                                   //取消专车
                                   $location.path('/cancel').search({
                                                                        'orderId': $routeParams.orderId,
                                                                        'type': $routeParams.type
                                                                    });
                               }
                           }

                       });

//取消订单
controllers.controller('CancelCtrl', function ($scope, $rootScope, $routeParams, $window, $location,
                                               daijiaService, zhuancheService, gobalService) {

    var nav = angular.element('nav');

    if (nav) {
        nav.hide();
    }

    if ($routeParams.type == 'daijia') {

        $scope.resons = [
            '我选择了其它交通方式',
            '司机让我等候太久',
            '司机无法来接我',
            '所在城市未开通服务'
        ]

    } else if ($routeParams.type == 'zhuanche') {

        $scope.resons = [
            '我改变了行程，不需要用车了',
            '我选择了其它交通方式',
            '司机让我等候太久',
            '司机无法来接我',
            '所在城市未开通服务'
        ]

    }

    $scope.hideCustom = true;

    $scope.checkMe = function (reson) {

        if (reson == '自定义') {
            $scope.hideCustom = false;
            $scope.reson = null;
        } else {
            $scope.hideCustom = true;
            $scope.reson = reson;
        }

    }

    var cfg = gobalService.config();

    //确定取消
    $scope.submit = function () {

        if ($scope.reson == null || $scope.reson == '') {
            alert('请选择或者输入取消原因');
            return;
        }

        if ($routeParams.type == 'daijia') {

            var promise = daijiaService.cancelOrder(cfg.sysKey, $routeParams.orderId, $scope.reson);

            promise.then(function (result) {
                if (result.code == 0) {
                    alert('销单成功');
                    //跳转主页面
                    $location.path('/index');
                } else {
                    alert(result.msg);
                }
            });
        }
        else if ($routeParams.type == 'zhuanche') {

            var promise = zhuancheService.cancelOrder(cfg.sysKey, $routeParams.orderId,
                                                      $scope.reson);
            promise.then(function (result) {

                if (result.success) {
                    alert('销单成功');
                    //跳转主页面
                    $location.path('/index');
                } else {
                    alert(result.message);
                }
            });

        }
    }

    //继续行程
    $scope.cancel = function () {
        $window.history.back();
    }

});

//订单执行中
controllers.controller('ExecuteCtrl',
                       function ($scope, $rootScope, $routeParams, $window, $location,
                                 daijiaService, zhuancheService, gobalService, driverService,
                                 locationService) {

                           var cfg = gobalService.config();

                           var nav = angular.element('nav');

                           if (nav) {
                               nav.hide();
                           }

                           $scope.hideStartDrive = true;
                           $scope.hideArrive = true;

                           var driverId = null;

                           /**
                            * 查询代驾订单
                            */
                           function findDJOrder() {

                               var promise = daijiaService.findOrder(cfg.sysKey,
                                                                     $routeParams.orderId);

                               promise.then(function (result) {
                                   if (result.success) {

                                       var orderInfo = result.data;


                                       if (orderInfo.status == 5) { //已经销单
                                           //跳转到主页
                                           $location.path('/index');
                                           $location.replace();
                                       }
                                       else if (orderInfo.status == 0 || orderInfo.status == 1) {

                                           //跳转下单成功界面
                                           $location.path('/success', {
                                               'orderId': $routeParams.orderId,
                                               'type': $routeParams.type
                                           });

                                       }
                                       else if (orderInfo.status == 2) { //司机已接单

                                           if (orderInfo.treatment) { //司机已经确认支付信息
                                               //跳转到支付界面
                                               $location.path('/payment').search({
                                                                                     'orderId': $routeParams.orderId,
                                                                                     'type': $routeParams.type
                                                                                 });
                                               return;
                                           }

                                           //显示订单信息
                                           $scope.title = "司机正赶往您的预约地";
                                           $scope.acceptTime = orderInfo.acceptTime; //接单时间

                                           $scope.arriveTime = orderInfo.arriveTime;//到达预约地时间
                                           if (orderInfo.arriveTime) {
                                               $scope.title = "司机已到达预约地";
                                               $scope.hideArrive = false;
                                           }

                                           $scope.startdriveTime = orderInfo.startdriveTime; //出发时间
                                           if (orderInfo.startdriveTime) {
                                               $scope.title = "司机正前往目的地";
                                               $scope.hideStartDrive = false;
                                           }

                                           //查询司机信息和位置信息
                                           if (driverId != orderInfo.driverId) {
                                               driverId = orderInfo.driverId;
                                               findDriverInfo(driverId, orderInfo.clientLatitude,
                                                              orderInfo.clientLongitude);
                                           }

                                           setTimeout(findDJOrder, 1000);//请求成功间隔一秒再次请求
                                       }
                                       else if (orderInfo.status == 3 || orderInfo.status == 6) { //已完成
                                           //跳转完成界面
                                           alert('订单已完成');
                                           $location.path("/index");
                                       }

                                   }

                               }, function (error) {

                                   setTimeout(findDJOrder, 1000);//请求成功间隔一秒再次请求

                               });

                           }

                           /**
                            * 查询专车订单
                            */
                           function findZCOrder() {

                               var promise = zhuancheService.findOrder(cfg.sysKey,
                                                                       $routeParams.orderId);

                               promise.then(function (result) {

                                   if (result.success) {

                                       var orderInfo = result.data;

                                       $scope.bookTime = orderInfo.serverTime;
                                       $scope.beginAddress = orderInfo.startAddress;
                                       $scope.endAddress = orderInfo.endAddress;
                                       $scope.phone = orderInfo.passengerPhone;

                                       if (orderInfo.status == 6) { //已经销单
                                           //跳转到主页
                                           $location.path('/index');
                                           $location.replace();
                                       }
                                       else if (orderInfo.status == 0 || orderInfo.status == 1) {

                                           //跳转到下单成功界面
                                           $location.path('/success', {
                                               'orderId': $routeParams.orderId,
                                               'type': $routeParams.type
                                           });

                                       }
                                       else if (orderInfo.status == 2 || orderInfo.status == 3
                                                || orderInfo.status == 4) { //司机已接单


                                           //显示订单信息
                                           $scope.title = "司机正赶往您的预约地";
                                           $scope.acceptTime = orderInfo.acceptTime; //接单时间

                                           if (orderInfo.status == 3) {
                                               $scope.arriveTime = orderInfo.arriveTime;//到达预约地时间
                                               $scope.title = "司机已到达预约地";
                                               $scope.hideArrive = false;
                                           }

                                           if (orderInfo.status == 4) {
                                               $scope.startdriveTime = orderInfo.startdriveTime; //出发时间
                                               $scope.title = "司机正前往目的地";
                                               $scope.hideStartDrive = false;

                                               if (orderInfo.treatment) { //跳转支付界面
                                                   $location.path('/payment').search({
                                                                                         'orderId': $routeParams.orderId,
                                                                                         'type': $routeParams.type
                                                                                     });
                                                   return;
                                               }
                                           }

                                           //查询司机信息和位置信息
                                           if (driverId != orderInfo.employId) {
                                               driverId = orderInfo.employId;
                                               findDriverInfo(driverId, orderInfo.startLat,
                                                              orderInfo.startLng);
                                           }

                                           setTimeout(findZCOrder, 2000);//请求成功间隔一秒再次请求

                                       }
                                       else if (orderInfo.status == 5) { //已完成
                                           //跳转完成界面
                                           alert('订单已完成');
                                           $location.path("/index");
                                       }

                                   }

                               }, function (error) {
                                   setTimeout(findZCOrder, 1000);//请求成功间隔一秒再次请求
                               });

                           }

                           /**
                            * 查询司机信息
                            *
                            * @param driverId 司机id
                            * @param lat  预约地纬度
                            * @param lng  预约地经度
                            */
                           function findDriverInfo(driverId, lat, lng) {

                               driverService.findDriver(cfg.sysKey, driverId)
                                   .then(function (result) {

                                       $scope.driverInfo = result;

                                       //计算预约地 与 司机位置距离

                                       locationService.geocoderFromBaidu({
                                                                             latitude: lat,
                                                                             longitude: lng
                                                                         }).then(function (data) {

                                           var addressComponent = data.addressComponent;
                                           var address = addressComponent.street
                                                         + addressComponent.street_number;
                                           $scope.currentAddress = address;

                                       });

                                   });

                           }

                           if ($routeParams.type == 'daijia') {

                               //查询订单信息
                               findDJOrder();

                           } else if ($routeParams.type == 'zhuanche') {

                               //查询专车订单
                               findZCOrder();
                           }

                           //取消订单
                           $scope.cancel = function () {

                               if ($routeParams.type == 'daijia') {
                                   //取消代驾
                                   $location.path('/cancel').search({
                                                                        'orderId': $routeParams.orderId,
                                                                        'type': $routeParams.type
                                                                    });
                               }
                               else if ($routeParams.type == 'zhuanche') {
                                   //取消专车
                                   $location.path('/cancel').search({
                                                                        'orderId': $routeParams.orderId,
                                                                        'type': $routeParams.type
                                                                    });
                               }
                           }
                       });

//时间选择
controllers.controller('DateTimePickerCtrl', function ($scope, $uibModalInstance) {

    var date = new Date();

    $scope.date = {
        year: date.getFullYear(),
        month: date.getMonth() + 1,
        day: date.getDate(),
        hour: date.getHours(),
        minute: date.getMinutes()
    };

    $scope.ok = function () {

        var date = new Date($scope.date.year, $scope.date.month
                                              - 1, $scope.date.day, $scope.date.hour, $scope.date.minute, 0);

        $uibModalInstance.close(date);
    }

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    }

    //分
    $scope.minuteAdd = function () {
        if ($scope.date.minute < 55) {
            $scope.date.minute = $scope.date.minute + 5;
        }
    }

    $scope.minuteSub = function () {
        if ($scope.date.minute > 0) {
            $scope.date.minute = $scope.date.minute - 5;
        }
    }

    //时
    $scope.hourAdd = function () {
        if ($scope.date.hour < 23) {
            $scope.date.hour++;
        }
    }

    $scope.hourSub = function () {
        if ($scope.date.hour > 0) {
            $scope.date.hour--;
        }
    }

    //年
    $scope.yearAdd = function () {

        $scope.date.year++;

        if ($scope.date.day > getLastDay()) {
            $scope.date.day = getLastDay();
        }
    }

    $scope.yearSub = function () {
        if ($scope.date.year > 2015) {
            $scope.date.year--;
        }

        if ($scope.date.day > getLastDay()) {
            $scope.date.day = getLastDay();
        }
    }

    //月
    $scope.monthAdd = function () {

        if ($scope.date.month < 12) {
            $scope.date.month++;
        }

        if ($scope.date.day > getLastDay()) {
            $scope.date.day = getLastDay();
        }
    }

    $scope.monthSub = function () {

        if ($scope.date.month > 1) {
            $scope.date.month--;
        }

        if ($scope.date.day > getLastDay()) {
            $scope.date.day = getLastDay();
        }
    }

    //日
    $scope.dayAdd = function () {

        if ($scope.date.day < getLastDay()) {
            $scope.date.day++;
        }

    }

    $scope.daySub = function () {
        if ($scope.date.day > 1) {
            $scope.date.day--;
        }
    }

    function getLastDay() {
        var time = new Date($scope.date.year, $scope.date.month, 1);
        return new Date(time.getTime() - 1000 * 60 * 60 * 24).getDate();
    }
});

//地址选择
controllers.controller('AddressPickerCtrl',
                       function ($scope, $rootScope, $uibModalInstance, locationService) {

                           $scope.items = [];

                           $scope.cancel = function () {
                               $uibModalInstance.dismiss('cancel');
                           }

                           $scope.select = function (pos) {

                               var poi = $scope.items[pos];

                               $uibModalInstance.close(poi);

                           }

                           $scope.$watch('address', function (newValue, oldValue, scope) {

                               var value = $.trim(newValue);

                               if (value.length > 2) {
                                   locationService.placeSearch(value,
                                                               $rootScope.currentLocation.city)
                                       .then(function (result) {
                                           if (result.data && result.data.status == 0) {
                                               $scope.items = result.data.results;
                                           }
                                       });
                               }
                           });
                       });

//提示消息框
controllers.controller('InfoCtrl', function ($scope, $uibModalInstance, content) {

    $scope.content = content;

    $scope.close = function () {
        $uibModalInstance.close();
    }
});

//支付界面
controllers.controller('PaymentCtrl',
                       function ($scope, $rootScope, $routeParams, $uibModal, $location,
                                 daijiaService, zhuancheService, gobalService) {

                           var cfg = gobalService.config();

                           $scope.isSuccess = false;

                           /**
                            * 检查代驾订单是否被支付
                            */
                           function checkDaijiaPay() {

                               if (!$scope.isSuccess) {

                                   var promise = daijiaService.hasPayed(cfg.sysKey,
                                                                        $routeParams.orderId);

                                   promise.then(function (result) {
                                       if (result.code == 0) {

                                           if (result.data) { //已经被支付
                                               if (!$scope.isSuccess) {
                                                   //弹出支付提示
                                                   var modalInstance = $uibModal.open({
                                                                                          templateUrl: 'assets/v2/partials/info.html',
                                                                                          controller: 'InfoCtrl',
                                                                                          resolve: {
                                                                                              content: function () {
                                                                                                  return '订单支付成功';
                                                                                              }
                                                                                          }
                                                                                      });
                                                   //跳转主页面
                                                   modalInstance.result.then(function (res) {
                                                       $location.path('/index');
                                                   });
                                               }

                                               $scope.isSuccess = true; //已经支付成功

                                           } else {
                                               setTimeout(checkDaijiaPay, 2000);//请求成功间隔2秒再次请求
                                           }

                                       } else {
                                           setTimeout(checkDaijiaPay, 2000);//请求成功间隔2秒再次请求
                                       }
                                   }, function (error) {

                                       setTimeout(checkDaijiaPay, 2000);//请求成功间隔2秒再次请求

                                   });

                               }

                           }

                           function checkZhuanchePay() {

                               if (!$scope.isSuccess) {

                                   var promise = zhuancheService.hasPayed(cfg.sysKey,
                                                                          $routeParams.orderId);

                                   promise.then(function (result) {
                                       if (result.code == 0) {

                                           if (result.data) { //已经被支付
                                               if (!$scope.isSuccess) {
                                                   //弹出支付提示
                                                   var modalInstance = $uibModal.open({
                                                                                          templateUrl: 'assets/v2/partials/info.html',
                                                                                          controller: 'InfoCtrl',
                                                                                          resolve: {
                                                                                              content: function () {
                                                                                                  return '订单支付成功';
                                                                                              }
                                                                                          }
                                                                                      });
                                                   //跳转主页面
                                                   modalInstance.result.then(function (res) {
                                                       $location.path('/index');
                                                   });
                                               }

                                               $scope.isSuccess = true; //已经支付成功

                                           } else {
                                               setTimeout(checkZhuanchePay, 2000);//请求成功间隔2秒再次请求
                                           }

                                       } else {
                                           setTimeout(checkZhuanchePay, 2000);//请求成功间隔2秒再次请求
                                       }
                                   }, function (error) {

                                       setTimeout(checkZhuanchePay, 2000);//请求成功间隔2秒再次请求

                                   });

                               }

                           }

                           if ($routeParams.type == 'daijia') {

                               //检查代驾订单是否已经被支付
                               checkDaijiaPay();

                               //获取支付信息
                               daijiaService.prepay(cfg.sysKey, $routeParams.orderId)
                                   .then(function (result) {

                                       if (result.code == 0) {
                                           var payInfo = result.data;
                                           $scope.payInfo = payInfo;
                                           $scope.totalFee =
                                               payInfo.realCash + payInfo.guoluMoney
                                               + payInfo.yuanchengMoney + payInfo.otherMoney;

                                       } else {
                                           alert(result.msg);
                                       }

                                   }, function (error) {
                                       alert('获取信息失败,请刷新页面重试');
                                   });

                           } else if ($routeParams.type == 'zhuanche') {

                               //查询专车订单
                               checkZhuanchePay();

                               //获取支付信息
                               zhuancheService.prepay(cfg.sysKey, $routeParams.orderId)
                                   .then(function (result) {

                                       if (result.code == 0) {
                                           var payInfo = result.data;
                                           $scope.payInfo = payInfo;
                                           $scope.totalFee =
                                               payInfo.realCash + payInfo.guoluMoney
                                               + payInfo.yuanchengMoney + payInfo.otherMoney;

                                       } else {
                                           alert(result.msg);
                                       }

                                   }, function (error) {
                                       alert('获取信息失败,请刷新页面重试');
                                   });

                           }

                           function payForDaijia() {

                               if ($scope.payType == 'weixin') { //微信

                                   daijiaService.doPayForWx(cfg.sysKey, $routeParams.orderId,
                                                            $rootScope.openId, cfg.appId)
                                       .then(function (result) {

                                           wx.config({
                                                         debug: false,
                                                         appId: cfg.appId,
                                                         timestamp: result.timestamp,
                                                         nonceStr: result.nonceStr,
                                                         signature: result.signature,
                                                         jsApiList: ['chooseWXPay']
                                                     });

                                           wx.ready(function () {
                                               wx.chooseWXPay({
                                                                  timestamp: result.timestamp,
                                                                  nonceStr: result.nonceStr,
                                                                  package: result.package,
                                                                  signType: 'MD5',
                                                                  paySign: result.paySign,
                                                                  success: function (res) {
                                                                      // 支付成功后的回调函数
                                                                      if (res.errMsg
                                                                          == 'chooseWXPay:ok') {
                                                                          if (!$scope.isSuccess) {
                                                                              //弹出支付提示
                                                                              var modalInstance = $uibModal.open(
                                                                                  {
                                                                                      templateUrl: 'assets/v2/partials/info.html',
                                                                                      controller: 'InfoCtrl',
                                                                                      resolve: {
                                                                                          content: function () {
                                                                                              return '订单支付成功';
                                                                                          }
                                                                                      }
                                                                                  });
                                                                              //跳转主页面
                                                                              modalInstance.result.then(
                                                                                  function (res) {
                                                                                      $location.path(
                                                                                          '/index');
                                                                                  });
                                                                          }
                                                                          $scope.isSuccess = true; //已经支付成功
                                                                      } else if (res.errMsg
                                                                                 == 'chooseWXPay:cancel') {
                                                                          alert('您取消了微信支付');
                                                                      } else {
                                                                          alert(res.errMsg);
                                                                      }
                                                                  }
                                                              });
                                           });

                                       });

                               }
                               else if ($scope.payType == 'balance' || $scope.payType == 'sign') { //余额 或者 //签单

                                   daijiaService.doPay(cfg.sysKey, $routeParams.orderId,
                                                       $scope.payType).then(function (result) {

                                       if (result.code == 0) {
                                           //弹出支付提示
                                           if (!$scope.isSuccess) {

                                               $scope.isSuccess = true;

                                               var modalInstance = $uibModal.open({
                                                                                      templateUrl: 'assets/v2/partials/info.html',
                                                                                      controller: 'InfoCtrl',
                                                                                      resolve: {
                                                                                          content: function () {
                                                                                              return '订单支付成功';
                                                                                          }
                                                                                      }
                                                                                  });
                                               //跳转主页面
                                               modalInstance.result.then(function (res) {
                                                   $location.path('/index');
                                               });
                                           }
                                       } else {
                                           alert(result.msg);
                                       }

                                   }, function (error) {
                                       alert('结算失败,请稍后再试');
                                   });

                               }
                           }

                           function payForZhuanche() {

                               if ($scope.payType == 'weixin') { //微信

                                   zhuancheService.doPayForWx(cfg.sysKey, $routeParams.orderId,
                                                              $rootScope.openId, cfg.appId)
                                       .then(function (result) {

                                           wx.config({
                                                         debug: false,
                                                         appId: cfg.appId,
                                                         timestamp: result.timestamp,
                                                         nonceStr: result.nonceStr,
                                                         signature: result.signature,
                                                         jsApiList: ['chooseWXPay']
                                                     });

                                           wx.ready(function () {
                                               wx.chooseWXPay({
                                                                  timestamp: result.timestamp,
                                                                  nonceStr: result.nonceStr,
                                                                  package: result.package,
                                                                  signType: 'MD5',
                                                                  paySign: result.paySign,
                                                                  success: function (res) {
                                                                      // 支付成功后的回调函数
                                                                      if (res.errMsg
                                                                          == 'chooseWXPay:ok') {
                                                                          if (!$scope.isSuccess) {
                                                                              //弹出支付提示
                                                                              var modalInstance = $uibModal.open(
                                                                                  {
                                                                                      templateUrl: 'assets/v2/partials/info.html',
                                                                                      controller: 'InfoCtrl',
                                                                                      resolve: {
                                                                                          content: function () {
                                                                                              return '订单支付成功';
                                                                                          }
                                                                                      }
                                                                                  });
                                                                              //跳转主页面
                                                                              modalInstance.result.then(
                                                                                  function (res) {
                                                                                      $location.path(
                                                                                          '/index');
                                                                                  });
                                                                          }
                                                                          $scope.isSuccess = true; //已经支付成功
                                                                      } else if (res.errMsg
                                                                                 == 'chooseWXPay:cancel') {
                                                                          alert('您取消了微信支付');
                                                                      } else {
                                                                          alert(res.errMsg);
                                                                      }
                                                                  }
                                                              });
                                           });

                                       });

                               }
                               else if ($scope.payType == 'balance' || $scope.payType == 'sign') { //余额 或者 //签单

                                   zhuancheService.doPay(cfg.sysKey, $routeParams.orderId,
                                                         $scope.payType).then(function (result) {

                                       if (result.code == 0) {
                                           //弹出支付提示
                                           if (!$scope.isSuccess) {

                                               $scope.isSuccess = true;

                                               var modalInstance = $uibModal.open({
                                                                                      templateUrl: 'assets/v2/partials/info.html',
                                                                                      controller: 'InfoCtrl',
                                                                                      resolve: {
                                                                                          content: function () {
                                                                                              return '订单支付成功';
                                                                                          }
                                                                                      }
                                                                                  });
                                               //跳转主页面
                                               modalInstance.result.then(function (res) {
                                                   $location.path('/index');
                                               });
                                           }
                                       } else {
                                           alert(result.msg);
                                       }

                                   }, function (error) {
                                       alert('结算失败,请稍后再试');
                                   });

                               }
                           }

                           $scope.dopay = function () {

                               if ($routeParams.type == 'daijia') {

                                   payForDaijia();

                               } else if ($routeParams.type == 'zhuanche') {

                                   payForZhuanche();
                               }

                           }

                       });

//登录界面
controllers.controller('LoginCtrl',
                       function ($scope, $rootScope, $sce, $routeParams, $interval, $location,
                                 gobalService, userService, locationService) {

                           var nav = angular.element('nav');

                           if (nav) {
                               nav.hide();
                           }

                           var refer = $routeParams.refer;
                           if (!refer) {
                               $location.path('/index');
                           }

                           var cfg = gobalService.config();

                           $scope.btn = $sce.trustAsHtml('获取验证码');
                           $scope.btnDisabled = false;

                           var promise = locationService.locateMe($rootScope);

                           promise.then(function (locationInfo) {
                               /**
                                * 发送验证码
                                */
                               $scope.sendVCode = function () {

                                   if (!$scope.phone) {
                                       alert('请输入手机号码');
                                       return;
                                   }

                                   $scope.btnDisabled = true;

                                   userService.sendVCode(cfg.sysKey, $scope.phone)
                                       .then(function (result) {

                                           if (result.success) {

                                               $scope.timecount = 120;

                                               var interval = $interval(function () {

                                                   $scope.timecount--;

                                                   $scope.btn =
                                                       $sce.trustAsHtml($scope.timecount + 's');

                                                   if ($scope.timecount == 0) {
                                                       $interval.cancel(interval);
                                                       $scope.btn = $sce.trustAsHtml('获取验证码');
                                                       $scope.btnDisabled = false;
                                                   }

                                               }, 1000);

                                           } else if (result.object) {

                                               $scope.timecount = 120 - parseInt(result.object);

                                               var interval = $interval(function () {

                                                   $scope.timecount--;

                                                   $scope.btn =
                                                       $sce.trustAsHtml($scope.timecount + 's');

                                                   if ($scope.timecount == 0) {
                                                       $interval.cancel(interval);
                                                       $scope.btn = $sce.trustAsHtml('获取验证码');
                                                       $scope.btnDisabled = false;
                                                   }

                                               }, 1000)
                                           } else {
                                               alert('验证码发送失败,请稍候再试');
                                               $scope.btnDisabled = false;
                                           }

                                       }, function (error) {
                                           alert('验证码发送失败,请稍候再试');
                                           $scope.btnDisabled = false;
                                       });
                               }

                               /**
                                * 执行登录
                                */
                               $scope.submit = function () {

                                   if (!$scope.phone) {
                                       alert('请输入手机号码');
                                       return;
                                   }

                                   if (!$scope.vcode) {
                                       alert('请输入验证码');
                                       return;
                                   }

                                   var promise = userService.checkCode(cfg.appId, cfg.sysKey,
                                                                       $scope.phone, $scope.vcode,
                                                                       $rootScope.openId,
                                                                       locationInfo.city);

                                   promise.then(function (result) {

                                       if (result.success) {
                                           $rootScope.passengerPhone = result.passengerPhone;
                                           $rootScope.passengerId = result.passengerId;
                                           $location.path(refer);

                                       } else {
                                           alert('登录失败,请稍候再试');
                                       }

                                   }, function (error) {
                                       alert('登录失败,请稍候再试');
                                   });
                               }

                           });

                       });

//跑腿其他订单
controllers.controller('OtherCtrl',
                       function ($scope, $rootScope, $routeParams, $sce, $uibModal, $filter,
                                 $location, gobalService, paotuiService, locationService,
                                 userService) {

                           $scope.task = {};

                           var nav = angular.element('nav');

                           if (nav) {
                               nav.show();
                           }

                           angular.element('ul.nav_line > li > a').removeClass('nav_click');
                           angular.element('#delivery').addClass('nav_click');

                           var cfg = gobalService.config();

                           if (isFromWx()) {
                               if ($rootScope.openId) {
                                   userService.findWxBind(cfg.sysKey, $rootScope.openId)
                                       .then(function (result) {
                                           if (result.code == 0) {
                                               var bindData = result.data;
                                               $rootScope.passengerId = bindData.passengerId;
                                               $rootScope.passengerPhone = bindData.phone;

                                               init();

                                           } else {
                                               //跳转登录界面
                                               $location.path('/login').search({
                                                                                   'refer': 'paotui/'
                                                                                            + $routeParams.action
                                                                               });
                                           }
                                       });
                               } else {
                                   $location.path('/index'); //跳转到首页
                               }

                           } else if (!$rootScope.passengerId) {
                               //跳转登录页面
                               $location.path('/login').search({
                                                                   'refer': 'paotui/'
                                                                            + $routeParams.action
                                                               });

                           } else {
                               init();
                           }

                           angular.element('#' + $routeParams.action).addClass('nav_click');

                           $scope.task = {actionName: '开始'};

                           var orderType = 'other';

                           if ($routeParams.action == 'buy') { //代买
                               $scope.task.actionName = '代买';
                               orderType = 'daimai';
                           } else if ($routeParams.action == 'line') { //排队
                               $scope.task.actionName = '排队';
                               orderType = 'paidui';
                           }

                           $scope.zhiding = true;

                           //tab 切换
                           $scope.setTab = function (name) {
                               if (name == 'one1') {
                                   $("#one1").addClass('off');
                                   $("#one2").removeClass('off');
                                   $("#con_one_1").show();
                                   $("#con_one_2").hide();
                                   $scope.zhiding = true;
                               } else if (name == 'one2') {
                                   $("#one2").addClass('off');
                                   $("#one1").removeClass('off');
                                   $("#con_one_1").hide();
                                   $("#con_one_2").show();
                                   $scope.zhiding = false;
                               }
                           }

                           //预算价格
                           function budget(locationInfo) {

                               if ($scope.task.startAddress && $scope.task.endAddress
                                   && $scope.zhiding) {
                                   //两个值都有的时候
                                   $scope.feemsg = "正在为您预估价格,请稍等...";
                                   //两个值都有的时候
                                   var promise = paotuiService.budget(cfg.sysKey, cfg.ack,
                                                                      locationInfo.city,
                                                                      $rootScope.passengerId,
                                                                      $scope.task.startLat,
                                                                      $scope.task.startLng,
                                                                      $scope.task.endLat,
                                                                      $scope.task.endLng);

                                   promise.then(function (result) {

                                       $scope.budget = result;


                                       if (result.success) {
                                           $scope.feemsg =
                                               $sce.trustAsHtml('<span>约<b>' + result.money
                                                                + '</b>元<img src="assets/v2/img/d-wh.png"/></span>');
                                           $scope.task.money = result.money;
                                       } else {
                                           $scope.feemsg = $sce.trustAsHtml('未能为您预估费用.');
                                       }

                                   }, function (error) {

                                       $scope.feemsg = $sce.trustAsHtml('未能为您预估费用.');

                                   });
                               }
                           }

                           function init() {

                               var promise = locationService.locateMe($rootScope);

                               promise.then(function (locationInfo) {

                                   //开始地址
                                   $scope.task.startAddressDetail = locationInfo.address;
                                   $scope.task.startAddress = locationInfo.name;
                                   $scope.task.startLat = locationInfo.point.lat;
                                   $scope.task.startLng = locationInfo.point.lng;
                                   //交付电话
                                   $scope.task.endPhone = $rootScope.passengerPhone;

                                   //选择时间
                                   $scope.showTimePicker = function () {

                                       var modalInstance = $uibModal.open({
                                                                              templateUrl: 'assets/v2/partials/datetimepicker.html',
                                                                              controller: 'DateTimePickerCtrl',
                                                                          });

                                       modalInstance.result.then(function (result) {
                                           $scope.task.endDate = result;//日期格式
                                           $scope.task.endTime =
                                               $filter('date')(result, 'yyyy-MM-dd HH:mm')
                                       });
                                   }

                                   //选择地址
                                   $scope.showAddressPicker = function () {

                                       var modalInstance = $uibModal.open({
                                                                              templateUrl: 'assets/v2/partials/addresspicker.html',
                                                                              controller: 'AddressPickerCtrl',
                                                                          });

                                       modalInstance.result.then(function (result) {

                                           $scope.task.startAddress = result.name;
                                           $scope.task.startLat = result.location.lat;
                                           $scope.task.startLng = result.location.lng;
                                           $scope.task.startAddressDetail = result.address;//具体地址名

                                           budget(locationInfo); //预算价格
                                       });
                                   }

                                   //选择交件地址
                                   $scope.showAddressPicker2 = function () {

                                       var modalInstance = $uibModal.open({
                                                                              templateUrl: 'assets/v2/partials/addresspicker.html',
                                                                              controller: 'AddressPickerCtrl',
                                                                          });

                                       modalInstance.result.then(function (result) {

                                           $scope.task.endLat = result.location.lat;
                                           $scope.task.endLng = result.location.lng;
                                           $scope.task.endAddressDetail = result.address;//具体地址名
                                           $scope.task.endAddress = result.name;

                                           budget(locationInfo); //预算价格
                                       });
                                   }

                                   //显示估价界面
                                   $scope.showPriceDetail = function () {

                                       var modalInstance = $uibModal.open({
                                                                              templateUrl: 'assets/v2/partials/info.html',
                                                                              controller: 'InfoCtrl',
                                                                              resolve: {
                                                                                  content: function () {
                                                                                      return 'hello world';
                                                                                  }
                                                                              }
                                                                          });
                                   }

                                   $scope.submit = function () {

                                       var msg = null;

                                       var params = $scope.task;

                                       if (!params.title || params.title.length == 0) {
                                           msg = '请先输入任务标题';
                                       } else if (!params.content || params.content.length == 0) {
                                           msg = '请先输入任务详情';
                                       } else if (!params.endTime || params.endTime.length == 0) {
                                           msg = '请先输入交件时间';
                                       } else if (!params.endPhone || params.endPhone.length == 0) {
                                           msg = '请先输入交件电话';
                                       } else if (!params.endAddress || params.endAddress.length
                                                                        == 0) {
                                           msg = '请先输入交件地址';
                                       }

                                       if ($scope.zhiding) { //指定取件地址
                                           if (!params.startAddress || params.startAddress.length
                                                                       == 0) {
                                               msg = '请先输入取件地址';
                                           }
                                       } else {//不指定
                                           if (!$scope.task.money) {
                                               msg = '请输入奖励金额';
                                           }
                                       }

                                       if (msg) {
                                           alert(msg);
                                           return;
                                       }

                                       if (!$scope.budget) {
                                           $scope.budget = {};
                                       }

                                       var deliverAddress = params.endAddress;
                                       if (params.endAddressDetail) {
                                           deliverAddress =
                                               deliverAddress + "," + params.endAddressDetail;
                                       }

                                       var startAddress = null;

                                       if ($scope.zhiding) { //指定地址
                                           startAddress = params.startAddress;
                                           if (params.startAddressDetail) {
                                               startAddress =
                                                   startAddress + "," + params.startAddressDetail;
                                           }
                                       }

                                       var pro = paotuiService.makeAnAppointment(params.title,
                                                                                 params.content,
                                                                                 params.endTime,
                                                                                 deliverAddress,
                                                                                 params.endLat,
                                                                                 params.endLng,
                                                                                 $rootScope.passengerPhone,
                                                                                 $rootScope.openId,
                                                                                 $rootScope.passengerId,
                                                                                 $scope.task.money,
                                                                                 params.startTime,
                                                                                 params.startPhone,
                                                                                 params.endPhone,
                                                                                 startAddress,
                                                                                 params.startLat,
                                                                                 params.startLng,
                                                                                 orderType,
                                                                                 $scope.budget.travelTime,
                                                                                 $scope.budget.mileage,
                                                                                 $scope.budget.startPrice,
                                                                                 $scope.budget.mileagePrice,
                                                                                 $scope.budget.travelTimePrice,
                                                                                 cfg.sysKey);

                                       pro.then(function (result) {
                                           if (result.code == 0) {
                                               alert('下单成功');
                                           } else {
                                               alert('下单失败');
                                           }

                                       }, function (error) {
                                           alert('下单失败,请稍候再试');
                                       });

                                   }
                               }, function (error) {
                                   if (error.code == 1) {
                                       alert('没有权限使用地理定位API');
                                   } else if (error.code == 2) {
                                       alert('无法确定设备的位置');
                                   } else if (error.code == 3) {
                                       alert('定位超时');
                                   }
                               });

                           }

                       });

//服务协议
controllers.controller('AgreementCtrl',
                       function ($scope, $rootScope, $sce, $window, userService, gobalService) {

                           var nav = angular.element('nav');
                           if (nav) {
                               nav.hide();
                           }

                           var cfg = gobalService.config();

                           userService.getAgreement(cfg.sysKey).then(function (result) {

                               $scope.agreement = $sce.trustAsHtml(result);

                           });

                           $scope.back = function () {
                               $window.history.back();
                           }

                       });

controllers.controller('AreaCtrl',
                       function ($scope, $rootScope, $sce, $window, areaService, gobalService) {

                           var params = $scope.task;

                           var nav = angular.element('nav');
                           if (nav) {
                               nav.hide();
                           }

                           var cfg = gobalService.config();

                           areaService.queryArea(cfg.sysKey, cfg.ack, params.passengerId,
                                                 params.lat, params.lng).then(function (result) {

                               $scope.area = $sce.trustAsHtml(result);

                           });

                           $scope.back = function () {
                               $window.history.back();
                           }

                       });